package cn.lili.controller.feign.order;

import cn.lili.common.utils.StringUtils;
import cn.lili.modules.order.order.client.OrderClient;
import cn.lili.modules.order.order.client.OrderFreeStatusLogClient;
import cn.lili.modules.order.order.entity.dos.OrderFreeStatusLog;
import cn.lili.modules.order.order.service.OrderFreeStatusLogService;
import cn.lili.modules.order.order.service.OrderService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 32312
 */
@RestController
@RequiredArgsConstructor
@Slf4j
public class OrderFreeStatusLogFeignController implements OrderFreeStatusLogClient {

    private final OrderFreeStatusLogService orderFreeStatusLogService;

    @Override
    public List<OrderFreeStatusLog> getBySn(String sn) {
        LambdaQueryWrapper<OrderFreeStatusLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderFreeStatusLog::getOrderSn, sn);
        return orderFreeStatusLogService.list(queryWrapper);
    }

    @Override
    public Boolean save(OrderFreeStatusLog orderFreeStatusLog) {
        log.info("feign保存订单状态日志 = {}", JSONObject.toJSONString(orderFreeStatusLog));
        if (orderFreeStatusLog != null && StringUtils.isNotEmpty(orderFreeStatusLog.getOrderStatus()) && StringUtils.isNotEmpty(orderFreeStatusLog.getOrderSn())) {
            LambdaQueryWrapper<OrderFreeStatusLog> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(OrderFreeStatusLog::getOrderSn, orderFreeStatusLog.getOrderSn());
            queryWrapper.eq(OrderFreeStatusLog::getOrderStatus, orderFreeStatusLog.getOrderStatus());
            long count = orderFreeStatusLogService.count(queryWrapper);
            if (count == 0) {
                orderFreeStatusLogService.save(orderFreeStatusLog);
            }
            return true;
        }
        return false;
    }

    @Override
    public List<OrderFreeStatusLog> getBySnAndOrderStatus(String outTradeNo, String orderStatus) {
        LambdaQueryWrapper<OrderFreeStatusLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderFreeStatusLog::getOutTradeNo, outTradeNo);
        queryWrapper.eq(OrderFreeStatusLog::getOrderStatus, orderStatus);
        return orderFreeStatusLogService.list(queryWrapper);
    }
}
